package com.fengba.nanzhi.myfengba;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
import com.fengba.nanzhi.fengba.R;
import com.fengba.nanzhi.login.Login;
import com.fengba.nanzhi.utils.FX_SignInUtil;
import com.fengba.nanzhi.utils.OKManager;
import com.fengba.nanzhi.utils.Preferences;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by SpakeBrony on 2018/4/13.
 */

public class MyDH extends Activity  {


    private RecyclerView mCardyhk;
    private JSONArray jsonArray1;


    public MyDH() {
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fx_my_dh);

        init();
        /**
         * 返回
         */
        ImageButton Nbtn01 = (ImageButton)findViewById(R.id.Mbtn01);
        Nbtn01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        final RefreshLayout refreshLayout = (RefreshLayout) findViewById(R.id.MrefreshLayout);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {

            public void onRefresh(RefreshLayout refreshlayout) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        if (jsonArray1 == null) {

                            Toast.makeText(MyDH.this, "已经没有更多数据了", Toast.LENGTH_SHORT).show();
                            refreshLayout.finishRefresh();
                        } else {
                            init();
                            Toast.makeText(MyDH.this, "查询成功", Toast.LENGTH_SHORT).show();
                            refreshLayout.finishRefresh();
                        }
                        /**
                         * 刷新完成后调用此方法，要不然刷新效果不消失
                         */

                    }
                }, 0);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
            }
        });


    }//都在这里打

    public void init() {
        /*final CountDownLatch latch = new CountDownLatch(1);
        OkHttpClient client = new OkHttpClient();
        FormBody formBody = new FormBody
                .Builder()
                .add("account", "17751567370")
                .add("type", "2")
                .add("status", "0")
                .build();
        final Request request = new Request.Builder()
                .url(UrlUtil.UrlFX + "api/Myorder/list")
                .post(formBody)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                latch.countDown();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {


                //final String responseStr = response.body().string();
                //ToastUtil.showToast(PostParameterActivity.this, "Code：" + String.valueOf(response.code()));
                try {
                    String json = response.body().string();
                    JSONObject jsonObject = new JSONObject(json);
                    if (jsonObject.get("code").equals(0)) {
                        jsonArray1 = jsonObject.getJSONArray("data");
                        //System.out.println("JA"+jsonArray1.toString());
                    } else {
                        new Runnable() {

                            @Override
                            public void run() {
                                //放在UI线程弹Toast
                                Toast.makeText(MyDH.this, "已经没有更多数据了", Toast.LENGTH_SHORT).show();
                            }
                        };
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                latch.countDown();
            }
        });
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
*/
        String account = null;
        try {
            Object user_info = Preferences.getParam(MyDH.this, "user_info", "");
            JSONObject myJson = null;
            myJson = new JSONObject(user_info.toString());
            account = myJson.get("account").toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (FX_SignInUtil.switchAccount(MyDH.this)) {
            startActivity(new Intent(MyDH.this, Login.class));
        } else {
            OKManager.getInstance().type2002(account, "2", "0", new OKManager.func4() {
                @Override
                public void onResponse(JSONObject jsonObject) {
                    try {
                        if (jsonObject.get("code").equals(0)) {
                            jsonArray1 = jsonObject.getJSONArray("data");
                            mCardyhk = findViewById(R.id.Mshdzlb);
                            mCardyhk.setLayoutManager(new LinearLayoutManager(MyDH.this));
                            mCardyhk.setAdapter(new MyDHAdapter(MyDH.this, MyDH.this));
                        } else {
                            //jsonArray1 = jsonObject.getJSONArray("data");
                            Toast.makeText(MyDH.this, jsonObject.get("message").toString(), Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(String string) {
                    Toast.makeText(MyDH.this, "网络错误", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
    public JSONArray getJsonArray() {
        return jsonArray1;
    }

}
